package com.shuzijun.leetcode.editor.cn;
public class InsertionSortList{
    public static void main(String[] args) {
         Solution solution = new InsertionSortList().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode insertionSortList(ListNode head) {
       if (head==null) return null;
        ListNode newHead = new ListNode(Integer.MIN_VALUE);

        ListNode p = head;
        while (p != null){
            ListNode tmp = p.next;
            ListNode q = newHead;
            while (q.next != null && q.next.val < p.val){
                q = q.next;
            }
            p.next = q.next;
            q.next = p;
            p=tmp;
        }
        return newHead.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}